package com.meatball.id

import com.meatball.core.config.id.SnowflakeIdWorker
import org.hibernate.engine.spi.SharedSessionContractImplementor
import org.hibernate.id.IdentifierGenerator
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component

/**
 * TODO 雪花ID生成器
 *
 * @property datacenterId
 * @property workerId
 */
@Component
class SnowflakeIdGenerator(
    @Value("\${snowflake.datacenter-id}") private val datacenterId: Long,
    @Value("\${snowflake.worker-id}") private val workerId: Long
) : IdentifierGenerator {
    override fun generate(session: SharedSessionContractImplementor?, `object`: Any?): Any {
        return SnowflakeIdWorker(
            datacenterId = datacenterId,
            workerId = workerId
        ).nextId()
    }
}